/**
 * Created by Panda on 16/01/2017
 */
(function () {
    'use strict';

    angular
        .module('com.akxx.aqzyk')
        .controller('trainController', trainController);


    /** @ngInject */
    function trainController($state,SweetAlert,_Train) {
        var vm = this;
        vm.newTrain =false;
        vm.overlay =false;
        vm.isVisit = false;
        vm.isReadonly = false;
        vm.trainItem = {};
        getLeftBarList({async: true,pageSize:0,pageIndex:25,sortExpr:"name desc"});
        function getLeftBarList(param) {
            _Train.TrainList.get(param).then(function (res) {
                vm.leftBarItems = res.data;
                vm.active = vm.leftBarItems[0].id;
                vm.rightContent = vm.leftBarItems[0].images;
            });
        };
        //关闭弹窗
        vm.closeFrame = function(){
            vm.newTrain = false;
            vm.overlay = false;
            vm.isReadonly = false;
        };

        //新建tab事件
        vm.addItemEvent = function(){
            vm.newTrain =true;
            vm.overlay =true;
            vm.saveType="new";
        };
        // 编辑tab事件
        vm.leftItemEdit=function(id){
            var param={id:id}
            _Train.VisitTrain.get(param).then(function (res) {
                if(res.success){
                    vm.trainItem = res.data;
                    vm.newTrain = true;
                    vm.overlay = true;
                    vm.saveType = "edit";
                    vm.saveId = id;
                }
            });
        }
        // 查看tab事件
        vm.leftItemVisit=function(id){
            var param={id:id}
            _Train.VisitTrain.get(param).then(function (res) {
                if(res.success){
                    vm.trainItem = res.data;
                    vm.newTrain = true;
                    vm.overlay = true;
                    vm.isReadonly = true;
                }
            });
        }
        //删除tab事件
        vm.leftItemDel = function (id) {
            var param={id:id}
            _Train.DelTrain.get(param).then(function (res) {
                if(res.success){
                    SweetAlert.swal("删除成功！")
                }
            });
        }
        // 保存
        vm.saveTrain = function () {
            if(vm.saveType = "new"){
                var param = vm.trainItem;
                _Train.NewTrain.get(param).then(function (res) {
                    if(res.success){
                        SweetAlert.swal("新建成功");
                        vm.newTrain =false;
                    }
                });
            }else if(vm.saveNavType = "edit"){
                var param = vm.trainItem;
                param.id = vm.saveId;
                _Train.EditTrain.get(param).then(function (res) {
                    if (res.success) {
                        SweetAlert.swal("编辑成功")
                        vm.newTrain =false;
                    }
                });
            }
            getTopNavList({async: true,pageSize:0,pageIndex:25,sortExpr:"name desc"});
        }

        //左侧划过事件
        vm.leftBarItemShow=function(id){
            vm.isShow = id;
            console.log(vm.isShow);
        }
        vm.leftBarItemHide=function(id){
            vm.isShow = "";
        }

        vm.leftItemEvent = function(id){
            vm.active = id;
            for(var i=0;i<vm.leftBarItems.length;i++){
                if(vm.leftBarItems[i].id==id){
                    vm.rightContent = vm.leftBarItems[i].images;
                }
            }
        }

    }
})();

